import React, {Component} from 'react';
import {messages} from "share/common";
import errorMessage from "share/errorMessage";
import {Badge, Button, message, Popconfirm} from "antd";
import ResizeTable from "components/resize-table";
import SlideFrame from "components/slide-frame";
import Info from "components/info";
import beepTimerService from "../beep-timer.service";
import RemindContent from "../slide-content/remind-content";
import BusinessCardContent from "../slide-content/business-card-content";
import RefundContent from "../slide-content/refund-content";

class RemindList extends Component {

  state = {
    loading: false,
  };

  columns = [{
    title: messages('common.sequence')/*序号*/,
    dataIndex: 'index',
    width: 72,
    render: (value, record, index) => index + 1
  }, {
    title: messages('basic-13.key249')/*层级*/,
    dataIndex: 'levelCode',
    width: 80,
    render: (value, record) => {
      let temp = value || JSON.parse(record.params).levelCode;
      return temp === 'TENANT' ? messages('basic-13.key256')/*租户*/ :
        temp === 'SET_OF_BOOK' ? messages('basic-13.key257')/*账套*/ : messages('basic-13.key43')/*公司*/
    }
  }, {
    title: messages('basic-13.key9')/*所属组织*/,
    dataIndex: 'levelOrgName',
    render: (value, record) => value || JSON.parse(record.params).levelOrgName
  }, {
    title: messages('basic-13.key16')/*消息标题*/,
    dataIndex: 'title',
    render: (value, record) => JSON.parse(record.params || '{}').title || '-'
  }, {
    title: messages('basic-13.key15')/*消息内容*/,
    dataIndex: 'content',
    render: (value, record) => JSON.parse(record.params || '{}').content || '-'
  }, {
    title: messages('common.column.status')/*状态*/,
    dataIndex: 'enabledFlag',
    width: 80,
    render: value => (
      <Badge
        status={value ? 'success' : 'error'}
        text={value ? messages('basic-13.key8')/*启用temp*/ : messages('basic-13.key31')/*禁用temp*/}
      />)
  }, {
    title: messages('common.operation')/*操作*/,
    dataIndex: 'id',
    width: 100,
    render: value => this.props.disabled ? '-' : (
      <Popconfirm
        placement="topRight"
        title={messages('common.confirm.delete')/*确定要删除吗？*/}
        onClick={e => e.stopPropagation()}
        onConfirm={() => this.handleDelete(value)}
      >
        <a>{messages('common.delete')/*删除*/}</a>
      </Popconfirm>
    )
  }];

  typeMap = {
    'EXPENSE_REPORT': {
      code: 'ExpenseReportNoticeTask',
      renderInfo: () => (<div>
        1、{messages('basic-13.key258')/*只对规则范围内的费用进行报销提醒*/} <br/>
        2、{messages('basic-13.key259')/*每笔费用可以设置多条提醒规则，每条规则都会发送提醒，即支持多次提醒*/} <br/>
        3、{messages('basic-13.key260')/*若未在系统中维护提醒对象数据，此条提醒将不会发送*/}
      </div>)
    },
    'EXPENSE_INVOICE': {
      code: 'ExpenseReportPasteReceiptNoticeTask',
      renderInfo: () => (<div>
        1、{messages('basic-13.key261')/*系统只对状态在规则范围内，且不处于已寄出、已收单、已退单阶段，且不含“免打印”标签的单据进行交票提醒。若单据状态在规则范围外，或处于已寄出、已收单、已退单阶段，或含 “免打印”标签，系统均不会进行交票提醒*/} <br/>
        2、{messages('basic-13.key262')/*每张报销单可以设置多条提醒规则，每条规则都会发送提醒，即支持多次提醒*/} <br/>
        3、{messages('basic-13.key260')/*若未在系统中维护提醒对象数据，此条提醒将不会发送*/}
      </div>)
    },
    'BUSINESS_CARD_REMIND': {
      code: 'BankCardTask',
      content: BusinessCardContent,
      renderInfo: () => (<div>
        {messages('basic-13.key263')/*1、对于指定账单月，且未生成费用或未提交报销单的商务卡消费数据，发送提醒*/} <br/>
        {messages('basic-13.key264')/*2、可以设置多条提醒规则，每条规则都会发送提醒，即支持多次提醒*/} <br/>
      </div>)
    },
    'REPAYMENT': {
      code: 'RepaymentNoticeTask',
      content: RefundContent,
      renderInfo: () => (<div>
        1、 {messages('basic-13.key301')/*只对未完成的还款的借款申请单提醒*/}<br/>
        2、 {messages('basic-13.key202')/*每个单据可以设置多条提醒规则，每条规则都会发送提醒，即支持多次提醒*/}
      </div>)
    }
  };

  handleEdit = record => {
    const { type } = this.props;
    SlideFrame.open({
      title: messages('basic-13.key195')/*消息设置*/,
      content: this.typeMap[this.props.type].content || RemindContent,
      params: type !== 'REPAYMENT' ? {
        info: record || {},
        type: this.props.type,
        disabled: this.props.disabled
      } : {
        info: record || {},
        record: record ? JSON.parse(record.params) : {}
      },
      afterClose: refresh => refresh && this.props.onRefresh()
    }, this)
  };

  handleDelete = id => {
    this.setState({ loading: true });
    beepTimerService.deleteTraveApprove([id])
      .then(() => {
        message.success(messages('common.delete.success',{arg1: ""})/*{arg1} 删除成功*/);
        this.props.onRefresh();
      })
      .catch(e => errorMessage(e.response))
      .finally(() => this.setState({ loading: false }))
  };

  render() {
    const { type, data, disabled } = this.props;
    const { loading } = this.state;
    const typeInfo = this.typeMap[type];
    const dataSource = data.filter(item => item.code === typeInfo.code);
    return (
      <div style={{ marginRight: 24 }}>
        <Info
          paragraph
          attributes={[{
            title: '',
            dataIndex: 'explain',
            span: 24,
            widthFix: false,
            colon: false,
            render: typeInfo.renderInfo
          }]}
        />
        <Button
          type="primary"
          disabled={disabled}
          style={{ margin: '12px 0' }}
          onClick={() => this.handleEdit()}
        >
          {messages('common.create')/*新建*/}
        </Button>
        <ResizeTable
          showUtils
          rowKey="id"
          loading={loading}
          columns={this.columns}
          dataSource={dataSource}
          tableName="remind-list-table"
          pagination={false}
          bordered
          onRow={record => ({
            onClick: () => this.handleEdit(record)
          })}
        />
      </div>
    );
  }
}

export default RemindList;
